package map_;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @Author 小闽
 * @Date 2024/11/13 22:20
 * @PackageName: map_
 * @ClassName: MapExercise
 * @Description:
 * @Version
 */
@SuppressWarnings({"all"})
public class MapExercise {
    public static void main(String[] args) {
        /**
         * 使用HashMap添加3个员工对象，要求
         * 键：员工id
         * 值：员工对象
         *并遍历显示工资 > 18000的员工（遍历方式最少两种）
         * 员工类：姓名，工资，员工id
         */
        Map map = new HashMap();
        map.put(1,new Employee("jack",3000,1));
        map.put(2,new Employee("tom",2000,2));
        map.put(3,new Employee("simth",1000,3));

        // 遍历的两种方式
        // 并遍历显示工资>1800的员工（遍历方式最少两种）
        // 1. 使用keySet ->增强for
        Set keySet = map.keySet();
        System.out.println("---第一种方式---");
        for (Object key : keySet) {
            // 先获取value
            Employee emp = (Employee)map.get(key);
            if (emp.getSal() > 1000){
                System.out.println(emp);
            }
        }
        // 使用EntrySet -> 迭代器
        Set entrySet = map.entrySet();
        System.out.println("===迭代器===");
        Iterator iterator = entrySet.iterator();
        while (iterator.hasNext()) {
            Map.Entry entry =  (Map.Entry) iterator.next();
            Employee emp = (Employee)entry.getValue();
            if (emp.getSal() > 2000){
                System.out.println(emp);
            }
        }
    }
}
class Employee{
    private String name;
    private double sal;
    private int id;

    public Employee(String name, double sal, int id) {
        this.name = name;
        this.sal = sal;
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", sal=" + sal +
                ", id=" + id +
                '}';
    }
}